package com.yangmao.mvc.config;

import com.google.gson.Gson;
import constant.SystemConstant;
import exception.LoginFailedException;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
import util.ResultEntity;
import util.SystemUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author ‘洋毛’
 * @version 1.0
 * @description: TODO
 * @date 2021/12/11 19:10
 */
// 标明该类是基于注解的异常处理器类
@ControllerAdvice
public class SystemExceptionResolver {

    // 处理登录异常
    @ExceptionHandler(value = {LoginFailedException.class})
    public ModelAndView resolveLoginFailedException(LoginFailedException exception,
                                                    HttpServletRequest request,
                                                    HttpServletResponse response)throws IOException
    {
        String viewName = "login";

        return commonCode(exception, request, response, viewName);
    }

    // 处理空指针异常
    @ExceptionHandler(value = {NullPointerException.class})
    public ModelAndView resolveNullPointerException(NullPointerException exception,
                                                    HttpServletRequest request,
                                                    HttpServletResponse response)throws IOException
    {
        return commonCode(exception, request, response, "error");
    }

    // 处理数学异常
    @ExceptionHandler(value = {ArithmeticException.class})
    public ModelAndView resolveArithmeticExceptio(ArithmeticException exception,
                                                    HttpServletRequest request,
                                                    HttpServletResponse response)throws IOException
    {
        return commonCode(exception, request, response, "error");
    }

    private ModelAndView commonCode(Exception exception,
                                    HttpServletRequest request,
                                    HttpServletResponse response, String viewName)throws IOException {

        boolean judgeRequestType = SystemUtil.judgeRequestType(request);

        if(judgeRequestType) {
            // 是json请求
            ResultEntity<Object> failed = ResultEntity.failed(exception.getMessage());
            // 创建Gson对象
            Gson gson = new Gson();
            // 将ResultEntity转换为json格式
            String json = gson.toJson(failed);
            // 通过原生的servlet的response返回异常数据
            response.getWriter().write(json);
            // 此时只需返回null，因为是通过json返回数据
            return null;

        }
        else{
            // 普通请求
            ModelAndView modelAndView = new ModelAndView();
            // 设置触发异常跳转的页面
            modelAndView.setViewName(viewName);
            // 将异常信息加入
            modelAndView.addObject(SystemConstant.ATTR_NAME_EXCEPTION,exception);
            // 返回
            return modelAndView;
        }
    }

}
